package MyTripBusServices;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.*;
import javax.swing.text.*;

class NumberTextField extends PlainDocument {
    public NumberTextField() {
        super();
    }
    public void insertString(int offset, String str, AttributeSet attr)
            throws javax.swing.text.BadLocationException {
        if (str == null) {
            return;
        }

        char[] s = str.toCharArray();
        int length = 0;
        // 过滤非数字
        for (int i = 0; i < s.length; i++) {
            if ((s[i] >= '0') && (s[i] <= '9')) {
                s[length++] = s[i];
            }
        // 插入内容
        super.insertString(offset, new String(s, 0, length), attr);
        }
    }
}

public class Registration extends JFrame implements ActionListener{
	JPanel panel;
	JLabel title, name, pass, contact, age, sex, mail, add;
	JTextField tname, tpass, tcontact, tage, tmail, tadd;
	JRadioButton male, female;
	JButton res, nex;
	
	Registration(){
		super("My Trip Bus Services");
		//Create a Panel
		panel=new JPanel();
		panel.setLayout(null);
		panel.setBackground(Color.white);
		add(panel);
		
		//Creating a label 'title'
		title=new JLabel("User Registration");
		title.setForeground(Color.red);
		title.setFont(new Font("Calibri",Font.BOLD,30));
		title.setBounds(175, 20, 300, 40);
		panel.add(title);
		
		//Creating a label 'name'
		name=new JLabel("Full Name:");
		name.setFont(new Font("Calibri",Font.PLAIN,20));
		name.setBounds(100, 100, 100, 20);
		panel.add(name);
		
		//Create a textfield
		tname=new JTextField();
		tname.setFont(new Font("Calibri",Font.PLAIN,15));
		tname.setBounds(250, 100, 180, 20);
		tname.setBackground(Color.yellow);
		panel.add(tname);
		
		//Creating a label 'pass'
		pass=new JLabel("Password:");
		pass.setFont(new Font("Calibri",Font.PLAIN,20));
		pass.setBounds(101, 150, 100, 20);
		panel.add(pass);
		
		//Create a textfield
		tpass=new JTextField();
		tpass.setFont(new Font("Calibri",Font.PLAIN,15));
		tpass.setBackground(Color.yellow);
		tpass.setBounds(250, 150, 180, 20);
		panel.add(tpass);
		
		//Creating a label 'contact'
		contact=new JLabel("Contact:");
		contact.setFont(new Font("Calibri",Font.PLAIN,20));
		contact.setBounds(104, 200, 100, 20);
		panel.add(contact);
				
		//Create a textfield
		tcontact=new JTextField();
		tcontact.setFont(new Font("Calibri",Font.PLAIN,15));
		tcontact.setBackground(Color.yellow);
		tcontact.setBounds(250, 200, 180, 20);
		tcontact.setDocument(new NumberTextField());
		panel.add(tcontact);
		
		//Creating a label 'age'
		age=new JLabel("Age:");
		age.setFont(new Font("Calibri",Font.PLAIN,20));
		age.setBounds(120, 250, 100, 20);
		panel.add(age);
						
		//Create a textfield
		tage=new JTextField();
		tage.setFont(new Font("Calibri",Font.PLAIN,15));
		tage.setBackground(Color.yellow);
		tage.setBounds(250, 250, 180, 20);
		panel.add(tage);
		
		//Creating a label 'gender'
		sex=new JLabel("Gender:");
		sex.setFont(new Font("Calibri",Font.PLAIN,20));
		sex.setBounds(110, 300, 100, 20);
		panel.add(sex);
								
		//Creating radiobuttons
		male=new JRadioButton("Male", true);
		male.setFont(new Font("Calibri",Font.PLAIN,18));
		male.setBounds(250, 300, 100, 20);
		panel.add(male);
				
		//Creating radiobuttons
		female=new JRadioButton("Female");
		female.setFont(new Font("Calibri",Font.PLAIN,18));
		female.setBounds(350, 300, 100, 20);
		panel.add(female);
				
		ButtonGroup bg = new ButtonGroup();bg.add(male);bg.add(female);
		
		//Creating a label 'email'
	    mail=new JLabel("Email ID:");
		mail.setFont(new Font("Calibri",Font.PLAIN,20));
		mail.setBounds(110, 350, 100, 20);
		panel.add(mail);
										
		//Create a textfield
		tmail=new JTextField();
		tmail.setFont(new Font("Calibri",Font.PLAIN,15));
		tmail.setBackground(Color.yellow);
		tmail.setBounds(250, 350, 180, 20);
		panel.add(tmail);
		
		//Creating a label 'address'
	    add=new JLabel("Address:");
		add.setFont(new Font("Calibri",Font.PLAIN,20));
		add.setBounds(110, 400, 100, 20);
		panel.add(add);
										
		//Create a textfield
		tadd=new JTextField();
		tadd.setFont(new Font("Calibri",Font.PLAIN,15));
		tadd.setBackground(Color.yellow);
		tadd.setBounds(250, 400, 180, 20);
		panel.add(tadd);
		
		//Creating button
		res=new JButton("Register");
		res.setFont(new Font("Calibri",Font.PLAIN,15));
		res.setBounds(150, 480, 120, 25);
		panel.add(res);
		
		//Creating button
		nex=new JButton("Next");
		nex.setFont(new Font("Calibri",Font.PLAIN,15));
		nex.setBounds(300, 480, 120, 25);
		panel.add(nex);
		
		res.addActionListener(this);
		nex.addActionListener(this);
		
		//Creating a frame
		setBounds(700, 50, 600, 600);
		setVisible(true);
		
	}
	public static void main(String[] args) {
		new Registration();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand()=="Register") {
			if(tname.getText().equals("")) {
				JOptionPane.showMessageDialog(this, "Name can't blank !!");
			}
			else {
				InsertData();
			}
		}
		if(e.getActionCommand()=="Next") {
			new usermodel();
			this.dispose();

		}
		
	}
	public static Connection createConnection() {
		Connection con=null;
     try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			con=DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/project?serverTimezone=UTC","root","131026");
		}
		catch(Exception e) {
			System.out.print(e);
		}
		return con;
		
	}
	public void InsertData() {
		try {
			String gender=male.isSelected()?"M":"F";
			Connection con=createConnection();
			String sql="insert into users(user_name,user_type,fullname,user_pwd,contact,age,gender,emailID,address) values('"+tname.getText()+"','member','"+tname.getText()+"','"+tpass.getText()+"','"+tcontact.getText()+"','"+tage.getText()+"','"+gender+"','"+tmail.getText()+"','"+tadd.getText()+"')";
			Statement stmt = con.createStatement();	
			int i=stmt.executeUpdate(sql);
			if(i!=0) {
				JOptionPane.showMessageDialog(this, "Registration Successful");
			    }
			else {
				JOptionPane.showMessageDialog(this, "Registration Failed");
			}
		}
		catch(Exception e) {	
			JOptionPane.showMessageDialog(this, e);
		}
	}
	}


